//
//  UpdateSexViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/15.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "UpdateSexViewController.h"
#import "UpdateSexTableViewCell.h"
#import "BLHeader.h"
#import "UpdateNet.h"
#import <MBProgressHUD.h>

@interface UpdateSexViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, assign) BOOL isGril;
@end

@implementation UpdateSexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.title = @"修改性别";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"UpdateSexTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:ReuseIdentify];
    
    NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:USERSEX];
    if(sex && [sex integerValue] == 1) {// 1. 男 2 女
        self.isGril = NO;
    }else if(sex && [sex integerValue] == 2) {
        self.isGril = YES;
    }
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(commitUpdate)];
    [right setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:kAppThemeColor ,NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = right;    
}



- (void)commitUpdate {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"性别只能修改一次哦！" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeIndeterminate;
        NSString *tel = [[NSUserDefaults standardUserDefaults] objectForKey:USERTEL];
        NSString *sex =[NSString stringWithFormat:@"%i", self.isGril ? 2 : 1];
        
        NSDictionary *param = @{@"userSex": sex , @"userTel": tel};
        
        __weak typeof (self) weakSelf = self;
        [UpdateNet updateUserInfo:param completer:^(id resultObject, NSError *error) {
            if(resultObject) {
                hud.mode = MBProgressHUDModeText;
                hud.label.text = @"修改成功！";
                [[NSUserDefaults standardUserDefaults] setObject:sex forKey:USERSEX];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                    [hud hideAnimated:YES];
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                });
                return ;
            }
            [hud hideAnimated:YES];
            
        }];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UpdateSexTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReuseIdentify];
    if( indexPath.row == 0) {
        [cell setupCell:@"男" isSelected:!self.isGril];
    }else {
        [cell setupCell:@"女" isSelected:self.isGril];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row == 0) {
        self.isGril = NO;
    }else {
        self.isGril = YES;
    }
    [self.tableView reloadData];
}

@end
